;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Maintenance.InstallBill", [])
        .controller("InstallBillCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "$timeout", "$filter", "ybConfig", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService, $timeout, $filter, ybConfig) {
            $scope.activeFlag = [true, false, false, false];
            $scope.subTabFlag = [true, false];
            $scope.datePickerFlag = [false, false];

            $scope.exportParam = {
              sectionParam: { billIds: [] }
            };

            $scope.markInfo = {
                allCheckedBill: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.condition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.dataList = [];
            $scope.orderObj = {};

            $scope.tableConfig = {
                fixedWidth: 100,
                scrollWidth: 1230,
                tableCol: [
                    {
                      name: "工单号",
                      dataField: "orderCode",
                      width: 120,
                      isShow: true,
                      isFixed: false,
                    },
                    {
                      name: "商城订单号",
                      dataField: "saleOrderCode",
                      width: 150,
                      isShow: true,
                      isFixed: false,
                      clickFn: "viewDetail",
                    },
                    {
                      name: "安装单状态",
                      dataField: "installationStatusShow",
                      width: 120,
                      isShow: true,
                      isFixed: false,
                    },
                    {
                      name: "商城订单状态",
                      dataField: "payStatusShow",
                      width: 120,
                      isShow: true,
                      isFixed: false,
                    },
                    {
                      name: "客户名称",
                      dataField: "customerName",
                      width: 120,
                      isShow: true,
                      isFixed: false,
                    },
                    {
                      name: "客户手机号",
                      dataField: "customerMobi",
                      width: 120,
                      isShow: true,
                      isFixed: false,
                    },
                    {
                      name: "代理商",
                      dataField: "dealerOrgName",
                      width: 180,
                      isShow: true,
                      isFixed: false,
                    },
                    {
                      name: "服务站",
                      dataField: "fwzName",
                      width: 180,
                      isShow: true,
                      isFixed: false,
                    },
                    {
                      name: "联系人",
                      dataField: "contact",
                      width: 120,
                      isShow: true,
                      isFixed: false,
                    },
                    {
                      type: "operate",
                      name: "操作",
                      width: 60,
                      isShow: true,
                      isFixed: true,
                    }
                ]
            };

            $scope.viewMaterielTableConfig = {
                scrollWidth: 1250,
                tableCol: [
                    {
                        name: "商品信息",
                        width: 250,
                        dataField: "productInfo",
                        isMerge: false
                    },
                    {
                        name: "数量",
                        width: 100,
                        dataField: "goodsNum",
                        isMerge: false
                    },
                    {
                        name: "状态",
                        width: 100,
                        dataField: "payStatusShow",
                        isMerge: true
                    },
                    {
                        name: "买家",
                        width: 150,
                        dataField: "customerInfo",
                        isMerge: true
                    },
                    {
                        name: "收货地址",
                        width: 250,
                        dataField: "orderAddressInfo",
                        isMerge: true
                    },
                    {
                        name: "安装点",
                        width: 250,
                        dataField: "installInfo",
                        isMerge: true
                    },
                    {
                        name: "留言",
                        width: 150,
                        dataField: "memo",
                        isMerge: true
                    },
                ]
            };

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {
                      left: $scope.tableConfig.fixedWidth + "px"
                    }
                };
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false];
                $scope.activeFlag[index] = true;
            };

            function getDataList(curPage) {
                $scope.markInfo = {
                    allCheckedBill: false,
                    curRow: -1
                };
                $scope.exportParam.sectionParam.billIds = [];


                var service = "mt";
                var reqUrl = "/installationOrder/list";
                
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }

                var param = {
                  pageSize: $scope.pageInfo.pageSize,
                  pageNum: $scope.pageInfo.curPage,
                  filters: angular.copy($scope.condition)
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.installationStatusShow = $filter("TrueOrFalse")(item.installationStatus, 'installStatus');
                        item.payStatusShow = $filter("PayStatus")(item.payStatus);
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {
                    seller: true
                };
            };

            function getBillIds() {
                $scope.exportParam.sectionParam.billIds = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.billIds.push(item.billId);
                    }
                });
            }

            $scope.selectSingle = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.markInfo["allChecked" + type] = false;
                } else {
                    if (!checkAll(list)) {
                        $scope.markInfo["allChecked" + type] = false;
                    } else {
                        $scope.markInfo["allChecked" + type] = true;
                    }
                }
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.markInfo["allChecked" + type];
                });
                getBillIds();
            };

            $scope.viewImage = function (item) {
              ybWidget.ViewImage({
                FileUrl: item,
              });
            };
            
            function getImgUrlShow(val) {
              return ybConfig.getConfig().baseUrl.ImgOssUrl + val;
            }

            function viewDetail({ saleOrderId }) {
              ybHttp.Get('mt', '/installationOrder/query4HfwOnlineOrderById', { saleOrderId }).then(function (resp) {
                if (resp.code !== 0) {
                    ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                    return;
                }
                const resT = resp.value.data || {};
                $scope.orderObj = resT;
                $scope.orderObj.materialsList = resT.orderSaleItemVOList;
                angular.forEach($scope.orderObj.materialsList, function (item) {
                  item.productInfo = {
                    goodsName: item.goodsName,
                    skuCode: item.skuCode,
                    imageUrlShow: getImgUrlShow(item.imageUrl),
                  };


                  item.payStatusShow = $filter("PayStatus")(resT.payStatus);
                  item.customerInfo = {
                    customerName: resT.customerName,
                    moboliePhone: resT.moboliePhone
                  };
                  item.orderAddressInfo = {
                    cname: resT.orderAddressVO.cname,
                    phone: resT.orderAddressVO.phone,
                    address: resT.orderAddressVO.provinceName + resT.orderAddressVO.cityName + resT.orderAddressVO.regionName + resT.orderAddressVO.detail
                  };
                  item.installInfo = {
                    stationContact: resT.stationContact,
                    stationContactTel: resT.stationContactTel,
                    stationAddress: resT.stationAddress
                  };
                  item.memo = resT.memo;
                });
                $scope.changeTab(1);
            });
            }

            $scope.handleFinish = function(item) {
              ybWidget.ShowMessage("确定要报完工吗？", MESG_TYPE.Ask).then(function () {
                ybHttp.Post("mt", "/installationOrder/updateFinish", item).then(function (resp) {
                  if (resp.code !== 0) {
                    ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                    return;
                  }
                  ybWidget.ShowToaster("success", "操作成功");
                  getDataList();
                });
              })
            };

            $scope.cancel = function () {
                $scope.changeTab(0);
            };


            (function () {
              $scope.setFixedWidth();
              getDataList();
            })();
        }]);
})(window, window.angular);
